﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace AppMachine.Controls.ValidationRules {
    public class ValidationRuleRangeNumber : ValidationRule {
        public double? MinValue { get; set; }
        public double? MaxValue { get; set; }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
            if (value == null) return ValidationResult.ValidResult;

            if (double.TryParse(value.ToString(), out double outValue)) {
                if (this.MinValue.HasValue) {
                    if (outValue < MinValue) {
                        return new ValidationResult(false, $"最小值为{MinValue}");
                    }
                }
                if (this.MaxValue.HasValue) {
                    if (outValue > MaxValue) {
                        return new ValidationResult(false, $"最大值为{MaxValue})");
                    }
                }
            }

            return ValidationResult.ValidResult;
        }

    }
}
